package com.itheima.delaytask;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by 传智播客*黑马程序员.
 */
@SpringBootApplication(scanBasePackages = {"com.itheima.delaytask","com.itheima.cache"})
@EnableScheduling
public class DelayTaskApplication {

    public static void main(String[] args) {
        SpringApplication.run(DelayTaskApplication.class,args);
    }


    @Bean(name = "delayTaskPool")
    public ThreadPoolTaskExecutor mythreadpool(){
        ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
        //设置核心线程数
        threadPool.setCorePoolSize(8);
        //设置最大线程数
        threadPool.setMaxPoolSize(10000);
        //设置线程超时等待时间
        threadPool.setKeepAliveSeconds(60);
        //设置任务等待队列的大小
        threadPool.setQueueCapacity(10000);
        //设置线程池内线程的名称前缀---阿里编码规约推荐----出错了方便调试
        threadPool.setThreadNamePrefix("delay-task-");
        //设置任务拒绝策略
        threadPool.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        //直接初始化
        threadPool.initialize();
        return threadPool;
    }
}
